package com.smartalarmclock.UI.Games;

import java.util.Random;

import com.smartalarmclock.UI.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MathExerciseActivity extends Activity{
	
	private TextView mathExerciseText;
	private Button buttonStopAlarm;
	private EditText result;
	private int firstNum = 0;
	private int secondNum = 0;
	private MediaPlayer m_MediaPlayer = new MediaPlayer();
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game_math_exercise);
		
			Random random = new Random();
			firstNum = random.nextInt(999-0);
			secondNum = random.nextInt(999-0);
			
			// A lot of copy code... will be fixed in later versions:
			try {
				Uri alert =  RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
				m_MediaPlayer.setDataSource(this, alert);
				final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
				if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0)
				{
					m_MediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
					m_MediaPlayer.setLooping(true);
					m_MediaPlayer.prepare();
					m_MediaPlayer.start();
					
				}
				
			} catch(Exception e) {
				
			}   
		
		addListenerOnButton();
	}

	@Override
	public void onBackPressed() { }
	
	private void addListenerOnButton() {
		mathExerciseText = (TextView) findViewById(R.id.textViewMtahExercise);
		buttonStopAlarm = (Button) findViewById(R.id.buttonStopAlarmOnMathChallenge);
		result = (EditText) findViewById(R.id.editTextResOnMathChallenge);
		
		mathExerciseText.setText(String.valueOf(firstNum) + "  +  " + String.valueOf(secondNum) + "  =");
		
		buttonStopAlarm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {			
				int res = 0;
				Editable value = result.getText();
				
				try {
					res = Integer.parseInt(value.toString());
				} catch(NumberFormatException nfe) {
					   Log.v("Math Exercise Activity", nfe.getMessage());
					} 
				
				
				if(firstNum + secondNum == res)
				{
					Intent resultIntent = new Intent();
					setResult(Activity.RESULT_OK, resultIntent);
					String msgToUser = "Equation is correct!\n Have a nice day";
					Toast.makeText(MathExerciseActivity.this, msgToUser, Toast.LENGTH_LONG).show();
					m_MediaPlayer.stop();
					finish();
				}
				
				else
				{					String msgToUser = "That's wrong!\n Please try again";
					Toast.makeText(MathExerciseActivity.this, msgToUser, Toast.LENGTH_LONG).show();
				}
					
			  }
			});
	}
}
